package net.sx.minemodule.mvp.presenters;

import com.kedll.library.mvp.BasePresenter;

import net.sx.minemodule.MineApiUtils;
import net.sx.minemodule.mvp.bean.EnumRefundType;
import net.sx.minemodule.mvp.bean.EnumSchedule;
import net.sx.minemodule.mvp.views.MineAfterRefundDetailView;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.concurrent.TimeUnit;

import io.reactivex.Observable;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;

/**
 * 退款/售后 详情页 1000013
 * Created by 冯多林 on 2019/7/4 0004.
 * Email:muzhi@uoko.com
 */
public class MineAfterRefundDetailPresenter extends BasePresenter<MineAfterRefundDetailView> {

    private Disposable dispoable;

    public MineAfterRefundDetailPresenter(MineAfterRefundDetailView view) {
        super(view);
    }

    public void initData(int refundId) {
        MineApiUtils.getRefundInfo("" + refundId, refundInfo -> {
            mView.loadDataSuccess(refundInfo);
        }, throwable -> {
            mView.loadDataFail(throwable.getMessage());
        });
    }

    /**
     * 撤销售后
     */
    public void cancelRefundOrder(String refundOrderId) {
        MineApiUtils.cancelRefundOrder(refundOrderId, refundInfo -> {
            mView.cancelSuccess();
        }, throwable -> {
            mView.cancelFail(throwable.getMessage());
        });
    }

    /**
     * 不同的售后有不同的流程
     * @param type 类型 退款，退货退款，换货
     * @return
     */
    public List<EnumSchedule> getStepData(int type) {
        ArrayList<EnumSchedule> objects = new ArrayList<>();
        if (type == EnumRefundType.REFUND.getKey()) {
            objects.add(EnumSchedule.REFUND_REQUEST);
            objects.add(EnumSchedule.REFUND_WAIT);
            objects.add(EnumSchedule.REFUND_SUCCESS);
        } else if (type == EnumRefundType.REFUND_GOODS.getKey()) {
            objects.add(EnumSchedule.RETURN_GOODS_REQUEST);
            objects.add(EnumSchedule.RETURN_GOODS_WAIT_RECEIVE);
            objects.add(EnumSchedule.RETURN_GOODS_WAIT_REFUND_5);
            objects.add(EnumSchedule.RETURN_GOODS_SUCCESS);
        } else if (type == EnumRefundType.CHANGE_GOODS.getKey()) {

        }
        return objects;
    }

    /**
     * 今后审核还剩多少秒倒计时
     *
     * @param parse    过期日期
     * @param refundId 售后订单ID
     */
    public void startCountDown(Date parse, int refundId) {
        if (parse.getTime() <= System.currentTimeMillis()) {
            mView.onTimeTick("已过期");
            return;
        }
        if (dispoable != null && !dispoable.isDisposed()) {
            dispoable.dispose();
        }
        dispoable = Observable.interval(1000, 1000, TimeUnit.MILLISECONDS)
                .observeOn(AndroidSchedulers.mainThread())
                .doOnComplete(() -> {
                    initData(refundId);
                })
                .subscribe(aLong -> {
                    long seconds = (parse.getTime() - System.currentTimeMillis()) / 1000;
                    if (seconds <= 0) {
                        dispoable.dispose();
                        initData(refundId);
                    } else {
                        long second = seconds % 60;
                        long minute = seconds / 60 % 60;
                        long hour = seconds / 60 / 60 % 24;
                        long day = seconds / 60 / 60 / 24;
                        mView.onTimeTick("还剩下" + day + "天" + hour + "小时" + minute + "分" + second + "秒");
                    }
                });
        addSubscription(dispoable);
    }
}
